<?php
session_start();
if (!isset($_SESSION['user'])) {
    header("Location: login.php");
    exit;
}

include '../service/productDetailService.php';

if (array_key_exists('msg', $_GET)) {
    $msg = $_GET['msg'];
}

if (array_key_exists('page_num', $_GET)) {
    $page_num = $_GET['page_num'];
}

if (!isset($page_num)) {
    $page_num = 1;
}
$row_per_page = 10;

$countDetail = countDetail();

$pages = ceil($countDetail / $row_per_page);
$start_position = ($page_num * $row_per_page) - ($row_per_page);

$search = '';
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <meta http-equiv="Content-type" content="text/html; charset=utf-8" />
        <title>Chi Tiet San Pham</title>
        <link rel="stylesheet" href="../css/admin-style.css" type="text/css" media="all" />
        <script type="text/javascript" src="http://code.jquery.com/jquery-1.4.2.min.js"></script>
        <script type="text/javascript" src="http://ajax.microsoft.com/ajax/jquery.validate/1.7/jquery.validate.min.js"></script>
        <script type="text/javascript">
            $(document).ready(function(){
                $("#addProAttrForm").validate(
                {
                    rules: {
                        name: "required",
                        description: "required"
                    },
                    messages: {
                        name: "Please name your product attribute.",
                        description: "Your attribute need a description"
                    },
                    submitHandler: function(form) {
                        // do other stuff for a valid form
                        $.post('process.php', $("#addProAttrForm").serialize(), function(data) {
                            //alert(data);
                            var nok = document.getElementById("msgNOK");
                            var ok = document.getElementById("msgOK");
                            if (data == 'added' || data == 'updated') {
                                formReset();

                                /*var p = document.getElementById("okMessage");
                                                        if (data == 'added') {
                                                                p.innerHTML = "User was added successfully";
                                                        } else {
                                                                p.innerHTML = "User was updated successfully";
                                                        }
							
                                                        if (hasClass(ok, 'msg-hidden')) {
                                                                removeClass(ok, 'msg-hidden');
                                                        }
                                                        if (!hasClass(nok, 'msg-hidden')) {
                                                                addClass(nok, 'msg-hidden ');
                                                        }*/

                                if (window.location.href.indexOf('msg') == -1) {
                                	// Check whether there is a '#' character at the end of the string.
                                    if (window.location.href.indexOf('#') == window.location.href.length - 1) {
                                    	window.location.replace(window.location.href.slice(0, -1) + '?msg=' + data);
                                    } else {
                                    	window.location.replace(window.location.href + '?msg=' + data);
                                    }
                                } else {
                                    window.location.reload();
                                }
                            } else if (data == 'NOK') {
                                if (hasClass(nok, 'msg-hidden')) {
                                    removeClass(nok, 'msg-hidden');
                                }
                                if (!hasClass(ok, 'msg-hidden')) {
                                    addClass(ok, 'msg-hidden');
                                }
                            }
                        });
                    }
                });
            });

            function confirmDelete(attrId) {
                answer = confirm("Do you want to delete this product attribute?");

                if (answer) {
                    $.post('process.php', 'deleteAttr=' + attrId, function(data) {
                        if (data == 'deleted') {
                            window.location.reload();
                        }
                    });
                }
            }

            function editAttr(id) {
                $.post('process.php', 'attrId=' + id, function(response) {
                    var attr = $.parseJSON(response);

                    var id = document.getElementById('attrId');
                    id.value = attr.PRD_ID;
				 
                    var type = document.getElementById('type');
                    type.value = attr.PRD_TYPE;

                    var name = document.getElementById('name');
                    name.value = attr.PRD_NAME;

                    var des = document.getElementById('description');
                    des.value = attr.PRD_DESCRIPTION;

                    var order = document.getElementById('order');
                    order.value = attr.PRD_SORTORDER;

                    var btn = document.getElementById('addAttr');
                    btn.value = 'Lưu thông tin';
                });
            }
		
            function deleteAll() {
                var chkAttrs = document.getElementsByName("chkProAttr");
                var attrs = new Array();
                for(var i = 0; i < chkAttrs.length; i++) {
                    if (chkAttrs[i].checked) {
                        attrs[attrs.length] = chkAttrs[i].id.substring(5);
                        chkAttrs[i].checked = false;
                    }
                }

                if (attrs.length > 0) {
                    answer = confirm("Do you want to delete selected attributes?");
                    if (answer) {
                        $.post('process.php', 'deleteAttr=' + attrs.join(','), function(data) {
                            if (data == 'deleted') {
                                window.location.reload();
                            }
                        });
                    }
                }
                var checkAll = document.getElementById("chkAll");
                checkAll.checked = false;
            }
		
            function hasClass(el, name) {
                return new RegExp('(\\s|^)'+name+'(\\s|$)').test(el.className);
            }

            function addClass(el, name)
            {
                if (!hasClass(el, name)) { el.className += (el.className ? ' ' : '') +name; }
            }

            function removeClass(el, name)
            {
                if (hasClass(el, name)) {
                    el.className=el.className.replace(new RegExp('(\\s|^)'+name+'(\\s|$)'),' ').replace(/^\s+|\s+$/g, '');
                }
            }
					
            function formReset() {
                document.getElementById("addProAttrForm").reset();
            }
		
            function toggle(source) {
                checkboxes = document.getElementsByName('chkProAttr');
                for(var i = 0; i < checkboxes.length; i++) {
                	checkboxes[i].checked = source.checked;
                }
            }

            function hideMessage(source) {
                addClass(source.parentNode, "msg-hidden");
            }
		
        </script>
    </head>
    <body>
        <!-- Header -->
        <?php include('header.php') ?>
        <!-- End Header -->

        <!-- Container -->
        <div id="container">
            <div class="shell">

                <!-- 
                <div class="small-nav">
                        <a href="#">Dashboard</a>
                        <span>&gt;</span>
                        Current Articles
                </div>
        Small Nav -->
                <!-- End Small Nav -->

                <!-- Main -->
                <div id="main">
                    <div class="cl">&nbsp;</div>

                    <!-- Content -->
                    <div id="content">

                        <!-- Box -->
                        <div class="box">
                            <!-- Box Head -->
                            <div class="box-head">
                                <h2 class="left">Danh Sách Chi Tiết Sản Phẩm</h2> 
                                <div class="right">
                                    <label>Tìm Kiếm:</label>
                                    <input type="text" class="field small-field" />
                                </div>
                            </div>
                            <!-- End Box Head -->	

                            <!-- Table -->
                            <div class="table">
                                <table width="100%" border="0" cellspacing="0" cellpadding="0">
                                    <tr>
                                        <th width="13"><input type="checkbox" class="checkbox" onclick="toggle(this)" id="chkAll"/></th>
                                        <th>STT</th>
                                        <th>Tên Chi Tiết Sản Phẩm</th>
                                        <th>Kiểu Dữ Liệu</th>
                                        <th>Ưu Tiên</th>
                                        <th width="110" class="ac">Thao Tác</th>
                                    </tr>
                                    <?php
                                    $pAttrs = sLoadDetailList($search, $start_position, $row_per_page);
                                    $counter = 1;
                                    while ($pAttr = mysql_fetch_array($pAttrs)) {
                                        ?>
                                        <tr>
                                            <td><input type="checkbox" class="checkbox" name="chkProAttr" <?php echo("id='pAttr" . $pAttr[0] . "'"); ?>/></th>
                                                <td><?php echo $counter; ?></td>
                                                <td><h3><?php echo("<a href='#' onclick='editAttr($pAttr[0])'>{$pAttr[1]}</a>"); ?></h3></td>
                                                <td><?php
                                    if ($pAttr[2] == 1) {
                                        echo 'Số';
                                    } else {
                                        echo 'Chữ';
                                    }
                                        ?></td>
                                                <td><?php echo($pAttr[3]); ?></td>
                                                <td><a href="#" class="ico del" onclick="confirmDelete(<?php echo($pAttr[0]); ?>)">Delete</a><a href="#" class="ico edit" onclick="editAttr(<?php echo($pAttr[0]); ?>)">Edit</a></td>
                                        </tr>
                                        <?php
                                        $counter++;
                                    }
                                    ?>

                                </table>


                                <!-- Pagging -->
                                <div class="pagging">
                                    <div class="left">
                                        <a href="#" class="ico del" onclick="deleteAll()"></a>
                                    </div>

                                    <div class="right">

                                        <?php
                                        for ($i = 1; $i <= $pages; $i++) {
                                            echo("<a href='productattribute.php?page_num=" . $i . "'" . ($page_num == $i ? "class='currentPage'" : "") . ">" . $i . "</a>");
                                        }
                                        ?>

                                    </div>
                                </div>
                                <!-- End Pagging -->

                            </div>
                            <!-- Table -->

                        </div>
                        <!-- End Box -->

                        <!-- Box -->
                        <div class="box">
                            <!-- Box Head -->
                            <div class="box-head">
                                <h2>Thao tác</h2>
                            </div>
                            <!-- End ofBox Head -->
                            <div class="box-content">
                                <!-- Message OK -->		
                                <div class="msg msg-ok <?php if (!isset($msg) || $msg == '')
                                            echo 'msg-hidden'; ?>">
                                    <p><strong>Sản phẩm đã được cập nhật</strong></p>
                                    <a class="close" onclick="hideMessage(this)">close</a>
                                </div>
                                <!-- End Message OK -->		

                                <!-- Message Error -->
                                <div class="msg msg-error msg-hidden">
                                    <p><strong>You must select a file to upload first!</strong></p>
                                    <a href="#" class="close" onclick="hideMessage(this)">close</a>
                                </div>
                                <!-- End Message Error -->

                                <form action="" method="post" id="addProAttrForm">

                                    <!-- Form -->
                                    <div class="form">
                                        <input type="hidden" id="attrId" name="attrId"/>
                                        <p>
                                            <label>Tên Chi Tiết Sản Phẩm</label>
                                            <input type="text" class="field size1" name="name" id="name" />
                                        </p>
                                        <p>
                                            <label>Thứ tự</label>
                                            <input type="text" class="field size1" name="order" id="order" value="<?php echo $countDetail + 1; ?>" />
                                        </p>
                                        <p>
                                            <label>Kiểu Dữ Liệu</label>
                                            <select class="field size1" name="type" id="type" >
                                                <option value="1">Số</option>
                                                <option value="2">Chữ</option>
                                            </select>
                                        </p>								
                                        <p>
                                            <label>Miêu tả </label>
                                            <textarea class="field size1" rows="10" cols="30" name="description" id="description" ></textarea>
                                        </p>	

                                    </div>
                                    <!-- End Form -->

                                    <!-- Form Buttons -->
                                    <div class="buttons">
                                        <input type="submit" class="button" value="Thêm Chi Tiết" id="addAttr" name="addAttr"/>
                                        <input type="reset" class="button" value="Xóa Biểu Nhập" />
                                        <input type="button" class="button" value="Trở Về" onclick="window.location = 'product.php'" />
                                    </div>
                                    <!-- End Form Buttons -->
                                </form>

                            </div>
                        </div>
                    </div>
                    <!-- End Content -->



                    <div class="cl">&nbsp;</div>			
                </div>
                <!-- Main -->
            </div>
        </div>
        <!-- End Container -->

        <!-- Footer -->
        <div id="footer">
            <?php include("footer.php"); ?>
        </div>
        <!-- End Footer -->

    </body>
</html>